﻿using System.Collections;
using com.youzu.warh.protocol;
using FlowCanvas.Nodes;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.AI;
using Yoozoo.Gta.Common;
using Yoozoo.Managers.ResourceManagerV2.Runtime;

namespace Yoozoo.Gameplay.Liberty.StreetEvent
{
    public class CreateTestSystemVehicle : LatentActionNode<string, bool>
    {
        private ResLoader _npcResLoader;
        private CarEntity carEntity;
        
        public override bool allowRoutineQueueing { get { return false; } }
        
        public override IEnumerator Invoke(string pointName, bool forceInCar)
        {
            var bluePoint = BlueprintUtils.GetBlueprintPoint(pointName.Trim());
            if (bluePoint == null)
            {
                yield break;
            }
            int carModelId = ResidentHandler.Inst.GetFunction("CityLuaFunction.getTestDriveCarId").Func<int, int>(0);
            bool npcLoadFinish = false;
            var npcManager = LibertyManager.GetInstance("LibertyManager").NpcManager;
            _npcResLoader = ResLoader.Alloc();
            PbAIPropertyDataConfig aiPropertyDataConfig = LibertyConfigManager.GetInstance().GetAIPropertyDataConfig(carModelId);
            if (aiPropertyDataConfig != null)
            {
                _npcResLoader.Add2Load(aiPropertyDataConfig.path, ((success, assetName, asset) =>
                {
                    if (!success)
                    {
                        return;
                    }
                    
                    npcManager.OnLoadNpc(aiPropertyDataConfig.id, aiPropertyDataConfig.type, aiPropertyDataConfig.name, asset as GameObject,1);
                }));
            }
            
            _npcResLoader.Load((() =>
            {
                npcLoadFinish = true;
            }));

            yield return new UnityEngine.WaitUntil(() => npcLoadFinish);
            
            CarBirthData birthData = new CarBirthData();
            birthData.random = carModelId == -1;
            birthData.carModelId = carModelId;
            birthData.driveNpcId = 0;
            birthData.position = bluePoint.position;
            birthData.forward = bluePoint.forward;
            
            bool success = LibertyAIUtils.AIManager.CarBirthManager.AddCarPure(birthData) != null;
            if (!success)
            {
                yield break;
            }

            carEntity = LibertyAIUtils.AIManager.CarBirthManager.GetCarEntity(birthData.uniqueId);
            if (carEntity == null)
            {
                yield break;
            }
       
            if (forceInCar)
            {
                // 玩家强制进车
                LibertyAIUtils.PlayerController.vehicleBehaviourMgr.EnterCarImmediately(carEntity,true);
            }
        }

        public override void OnGraphStoped()
        {
            base.OnGraphStoped();
            if (carEntity != null)
            {
                LibertyAIUtils.AIManager.CarBirthManager.RemoveCar(carEntity.id);
            }

            if (_npcResLoader != null)
            {
                _npcResLoader.Recycle2Cache();
                _npcResLoader = null;
            }
        }
    }
}